package com.example.newapplication.Fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.newapplication.Important_fragment.ImprotantFragment;
import com.example.newapplication.Important_fragment.ImprotantFragment2;
import com.example.newapplication.R;
import com.example.newapplication.monitor;
import com.getbase.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;


public class ImportentFragment extends Fragment {

    private FloatingActionButton bt_1,bt_2;
    private ImageView left,right,person;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private TextView text1,text2,text3,text4,text5,text6,text7,text8,text9,text10,monitorText;

    private int num = 0;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_importent, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        monitorText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), monitor.class);
                startActivity(intent);
            }
        });
        person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("当前拥有者")
                        .setIcon(R.mipmap.jinggao)
                        .setMessage("借用者名称：唐三皮                                                  借用者编号：201801020217                                                  当前位置：319特别仓库")
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                }).show();

            }
        });
        fragments.add(new ImprotantFragment());
        fragments.add(new ImprotantFragment2());

        text1.setText("A号仓库（不可移动）");
        text2.setText("正在使用");
        text3.setText("2021年1月1日");
        text4.setText("2021年4月9日");
        text5.setText("20210411");
        text6.setText("需要设备");
        text7.setText("A号使用者");
        text8.setText("20210412");
        text9.setText("允许借用");
        text10.setText("B号处理者");

        viewPager.setAdapter(new FragmentAdapter( getChildFragmentManager() , FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments));

        viewPager.setCurrentItem(num);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(num==1){
                    viewPager.setCurrentItem(0);
                    num = 0;
                    text1.setText("A号仓库（不可移动）");
                    text2.setText("正在使用");
                    text3.setText("2021年1月1日");
                    text4.setText("2021年4月9日");
                    text5.setText("20210411");
                    text6.setText("需要设备");
                    text7.setText("A号使用者");
                    text8.setText("20210412");
                    text9.setText("允许借用");
                    text10.setText("B号处理者");
                }
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(num == 0){
                    viewPager.setCurrentItem(1);
                    num = 1;
                    text1.setText("C号仓库（不可移动）");
                    text2.setText("设备空闲");
                    text3.setText("2021年2月2日");
                    text4.setText("2021年4月11日");
                    text5.setText("20210415");
                    text6.setText("需要设备");
                    text7.setText("B号使用者");
                    text8.setText("20210415");
                    text9.setText("拒绝借用");
                    text10.setText("C号处理者");
                }
            }
        });

    }

    private void init(){
        left = getActivity().findViewById(R.id.imageView21);
        right = getActivity().findViewById(R.id.imageView23);
        bt_1 = getActivity().findViewById(R.id.fab_1);
        bt_2 = getActivity().findViewById(R.id.fab_2);
        viewPager = getActivity().findViewById(R.id.viewPager);
        person = getActivity().findViewById(R.id.imageView25);
        //下方数据
        text1 = getActivity().findViewById(R.id.textView95);
        text2 = getActivity().findViewById(R.id.state);
        text3 = getActivity().findViewById(R.id.maintain);
        text4 = getActivity().findViewById(R.id.finaldata);
        text5 = getActivity().findViewById(R.id.textView117);
        text6 = getActivity().findViewById(R.id.textView116);
        text7 = getActivity().findViewById(R.id.textView118);
        text8 = getActivity().findViewById(R.id.textView120);
        text9 = getActivity().findViewById(R.id.textView119);
        text10 = getActivity().findViewById(R.id.textView121);
        monitorText = getActivity().findViewById(R.id.monitortext);
    }

    private class FragmentAdapter extends FragmentStatePagerAdapter {
        List<Fragment> list;
        public FragmentAdapter(@NonNull FragmentManager fm, int behavior,ArrayList<Fragment> fragments) {
            super(fm, behavior);
            this.list = fragments;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public Parcelable saveState() {
            return null;
        }
    }
}